﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.Linq;

namespace ReserveMeRIAWCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {        

        /// <summary>
        /// Get a List of <list type="ReserveMeBuilding"/> for all buildings
        /// contained within the database
        /// </summary>
        /// <returns>List of ReserveMeBuilding</returns>
        public List<ReserveMeBuilding> getBuildings()
        {  
            using (RMDBDataContext rms = new RMDBDataContext())
            {
                rms.DeferredLoadingEnabled = false;
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<ReserveMeBuilding>(t => t.Name);
                rms.LoadOptions = dlo;
                var buildings = (from building in rms.ReserveMeBuildings select building).ToList();
                return buildings.Take(buildings.Count).ToList();
            }
        }

        /// <summary>
        /// Get a List of <list type="ReserveMeRoom"/> for all rooms
        /// contained within the database
        /// </summary>
        /// <returns>List of ReserveMeRoom</returns>
        public List<ReserveMeRoom> getRooms()
        {
            using (RMDBDataContext rms = new RMDBDataContext())
            {
                rms.DeferredLoadingEnabled = false;
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<ReserveMeRoom>(t => t.RoomName);
                rms.LoadOptions = dlo;
                var rooms = (from room in rms.ReserveMeRooms select room).ToList();
                return rooms;
            }
        }

        /// <summary>
        /// Get a List of <list type="ReserveMeRoom"/> for all rooms
        /// from a specific building contained within the database 
        /// </summary>
        /// <param name="buildingID">the ID of the Building</param>
        /// <returns>List of ReserveMeRoom</returns>
        public List<ReserveMeRoom> getRoomsBuilding(int buildingID)
        {           
            using (RMDBDataContext rms = new RMDBDataContext())
            {
                rms.DeferredLoadingEnabled = false;
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<ReserveMeRoom>(t => t.RoomName);
                rms.LoadOptions = dlo;
                var rooms = (from room in rms.ReserveMeRooms where room.BuildingID == buildingID select room).ToList();
                return rooms;
            }
        }

        /// <summary>
        /// Get a List of <list type="ReserveMeComputer"/> for all computers
        /// from a specific building
        /// </summary>
        /// <param name="buildingID">the ID of the building</param>
        /// <returns>List of <list type="ReserveMeComputer"</returns>
        public List<ReserveMeComputer> getComputerBuilding(int buildingID)
        {           
            using (RMDBDataContext rms = new RMDBDataContext())
            {
                rms.DeferredLoadingEnabled = false;
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<ReserveMeComputer>(t => t.Tag);
                rms.LoadOptions = dlo;
                var computers = (from computer in rms.ReserveMeComputers where computer.ReserveMeRoom.BuildingID == buildingID select computer).ToList();
                return computers;
            }
        }

        /// <summary>
        /// Get a List of <list type="ReserveMeComputer"/> for all computers
        /// within a room
        /// </summary>
        /// <param name="roomID">ID of the room</param>
        /// <returns>List of <list type="ReserveMeComputer"/></returns>
        public List<ReserveMeComputer> getComputers(int roomID)
        {           
            using (RMDBDataContext rms = new RMDBDataContext())
            {
                rms.DeferredLoadingEnabled = false;
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<ReserveMeComputer>(t => t.Tag);
                rms.LoadOptions = dlo;
                var computers = (from computer in rms.ReserveMeComputers where computer.RoomID == roomID select computer).ToList();
                return computers;
            }
        }

        public List<getApplicationsInstalledAllComputer> getApplicationInstalled(int cid)
        {
            using (RMDBDataContext rms = new RMDBDataContext())
            {
                rms.DeferredLoadingEnabled = false;
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<getApplicationsInstalledAllComputer>(t => t.ComputerID);
                rms.LoadOptions = dlo;
                var list = (from application in rms.getApplicationsInstalledAllComputers where application.ComputerID == cid select application).ToList();
                return list;
            }
        }
    }
}
